package pro.softzhang.algo.lc700;

import pro.softzhang.algo.flag.knowledge.KBinarySearch;

/**
 *
 */
public class LC704_BinarySearch {
    public static void main(String[] args) {
        int[] nums = {-1,0,3,5,9,12};
        int target = 9;
        System.out.println(new Solution().search(nums, target));
    }

    @KBinarySearch
    static
    class Solution {
        public int search(int[] nums, int target) {
            for(int l = 0, r = nums.length - 1; l <= r; ) {
                int m = l + (r - l) / 2;

                if (nums[m] == target) {
                    return m;
                }

                if (nums[m] > target) {
                    r = m - 1;
                } else {
                    l = m + 1;
                }
            }

            return -1 ;
        }
    }
}
